import api from '@/api/shop'
import { Message } from 'element-ui';
const shop = {
    state: {
        visible: false,
        goodsDatas: {
            template: [],
            component: [],
            pictrue: [],
            icon: [],
            sdk: []
        },
        searchData: {},
        pagination: {
            total:  null, //总共有几个
            pageNum: 1, //当前页数
            pageSize: 12, //一个展示几个
        },
        reload: true,
        Property: {
            template_version: '',
            template_type: '',
            template_lead: '',
            template_display: '',
            component_version: '',
            component_type: '',
            component_lead: '',
            component_display: '',
            pictrue_mode: '',
            pictrue_ec: '',
            pictrue_size: '',
            pictrue_format: '',
            icon_mode: '',
            icon_type: '',
            icon_number: '',
            icon_format: '',
            sdk_type: '',
            sdk_drive: '',
            sdk_size: '',
            sdk_version: ''
        },
        Form: {
            goods_name: '', //名称
            goods_price: '', //价格
            goods_description: '', //描述,
            goods_updateLog: '', //更新
            type_id: null,
            shop_id: null,
            propertyValueItemReqVOList: null,
        },
        titleImg: {
            src: '',
            name: ''
        },
        contentImg: [],
        fileText: [],
        shopDes: {
            service: null,
            description: null,
            shop_name: null,
            isCollect: false,
            shop_id: null
        },
    },

    mutations: {
        saveVisible: (state, payload) => {
            state.visible = payload.visible;
            state.Form = {
                goods_name: '', //名称
                goods_price: '', //价格
                goods_description: '', //描述,
                goods_updateLog: '', //更新
                type_id: null,
                shop_id: null,
                propertyValueItemReqVOList: null,
            },
            state.titleImg = { src: '', name: '' };
            state.fileText.splice(0,state.fileText.length);
            state.contentImg.splice(0,state.contentImg.length);
        },
        saveMyShop: (state, payload) => {
            Object.keys(state.goodsDatas).forEach(key => {
                state.goodsDatas[key] = [];
            });
            payload.goodsList.forEach(item => {
                payload.goodsNum.forEach(num => {
                    if(num.goods_id === item.goods_id){
                        Object.assign(item, {...num});
                    }
                })
            })
            payload.goodsList.forEach(item => {
                switch(item.type_name){
                    case '模板':
                        state.goodsDatas.template.push(item);
                        break;
                    case '组件':
                        state.goodsDatas.component.push(item);
                        break;
                    case '图片':
                        state.goodsDatas.pictrue.push(item);
                        break;
                    case '图标':
                        state.goodsDatas.icon.push(item);
                        break;
                    case 'sdk':
                        state.goodsDatas.sdk.push(item);
                        break;
                }
            });
        },
        saveSearchData: (state, {search_content, goodsList}) => {
            state.searchData = Object.assign({},{ [`与 ${search_content} 相关的宝贝`]: [...goodsList] });
        },
        savePagination: (state, paylaod) => {
            state.pagination = Object.assign(state.pagination,paylaod);
        },
        //先设置为false 再设置为true 手动更新分页数据 不可同时设置
        reloadPagination: (state, bool) => {
            state.reload = bool;
        },
        clearFiles: (state) => {
            state.titleImg = {
                src: '',
                name: ''
            };
            state.contentImg.splice(0,state.contentImg.length);
            state.fileText.splice(0,state.fileText.length);
        },
        saveShopInfo: (state, payload) => {
            Object.assign(state.shopDes, payload);
        }
    },

    actions: {
        //vuex不方便操作model
        fetchUpdateGoodsInfo({ commit },payload){
            return api.getUpdateGoodsInfo(payload);
        },
        async fetchShopGoods({ commit },payload) {
            const { code, extend: { result } } = await api.getShopGoods(payload);
            if(code === 200){
                commit('saveMyShop', result);
            }
        },  
        async fetchShopGoodsByType({ commit },payload) {
            const { code, extend: { result } } = await api.getShopGoodsByType(payload);
            if(code === 200){
                commit('saveMyShop', {goodsNum: result.goodsNum, goodsList: result.goodsList.list});
                commit('savePagination', {
                    total:  result.goodsList.total,  //总共有几个
                    pageNum: result.goodsList.pageNum,  //当前页数
                });
                commit('reloadPagination',true);
            }
        },
        //需要在页面进行接口状态判断
        async fetchSetGoods({ _ },payload) {
            const { code } = await api.setGoods(payload);
            // if(code === 200){
            //     console.log('商品文字添加成功');
            // }else{
            //     console.log('商品文字添加失败');
            // }
        },
        async fetchSearchGoodsInShop({ commit },payload) {
            const { code, extend: { result } } = await api.searchGoodsInShop(payload);
            if(code === 200){
                commit('saveSearchData',{search_content: payload.search_content, goodsList: result.goodsList})
            }
            if(code === 100){
                commit('saveSearchData',{search_content: payload.search_content, goodsList: []})
            }
        },
        async fetchGoodsUpshelf({ commit },payload) {
            const { code } = await api.goodsUpshelf(payload);
            if(code === 200){
                Message({
                    type: 'success',
                    showClose: true,
                    message: '您已成功上架该商品。'
                });
            }else{
                Message({
                    type: 'error',
                    showClose: true,
                    message: '上架失败,请重试'
                });
            }

        },
        async fetchGoodsDownshelf({ commit },payload) {
            const { code } = await api.goodsDownshelf(payload);
            if(code === 200){
                Message({
                    type: 'success',
                    showClose: true,
                    message: '宝贝已下架。'
                });
            }else{
                Message({
                    type: 'error',
                    showClose: true,
                    message: '宝贝下架失败,请重试'
                });
            }

        },
        async fetchUpdateGoods({ commit },payload) {
            const { code } = await api.updateGoodsInfo(payload);
            if(code === 200){
                Message({
                    type: 'success',
                    showClose: true,
                    message: '基本信息修改成功。'
                });
            }
        },
        async fetchShopScore({ commit },payload) {
            const { code, extend: { result } } = await api.getShopScore(payload);
            if(code === 200){
                commit('saveShopInfo',result);
            }
        },
    }
}

export default shop